package com.harusource.test;

import com.harusource.di.annotation.Event;
import com.harusource.di.core.activity.HaruListActivity;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

/**
 * Sample of using HaruListActivity
 * If you want to use @Inject and @Event annotation to inject some dependency,
 * you must create a your Activity from HaruActivity instead of Activity.
 * @author Hiroki Oizumi
 *
 */
public class SampleHaruListActivity extends HaruListActivity{
    ArrayAdapter<String> mAdapter;
    private String[] mStrings = { "aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt", "uuu"};

    SharedPreferences mPref;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        
        mPref = PreferenceManager.getDefaultSharedPreferences(this);
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
        setListAdapter(mAdapter);
    }
    
    @Event(clazz=AbsListView.OnScrollListener.class, id=android.R.id.list)
    void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
        Log.d(getClass().getSimpleName(), String.format("onScroll : firstItem=%d, visibleCount=%d, totalCount=%d", firstVisibleItem, visibleItemCount, totalItemCount));
        setProcessed("HaruListActivity#onScroll");
    }
    
    @Event(clazz=AbsListView.OnScrollListener.class, id=android.R.id.list)
    public void onScrollStateChanged(AbsListView view, int scrollState){
        Log.d(getClass().getSimpleName(), String.format("onScrollStateChanged : state=%d", scrollState));
        setProcessed("HaruListActivity#onScrollStateChanged");
    }
    
    @Event(clazz=AbsListView.RecyclerListener.class, id=android.R.id.list)
    public void onMovedToScrapHeap(View view){
        Log.d(getClass().getSimpleName(), "onMovedToScrapHeap");
        setProcessed("HaruListActivity#onMovedToScrapHeap");
    }
    
    @Event(clazz=AdapterView.OnItemClickListener.class, id=android.R.id.list)
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Log.d(getClass().getSimpleName(), String.format("onItemClick, pos=%d, id=%d", arg2, arg3));
        setProcessed("HaruListActivity#onItemClick");
    }
    
    @Event(clazz=AdapterView.OnItemLongClickListener.class, id=android.R.id.list)
    boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
        Log.d(getClass().getSimpleName(), String.format("onItemLongClick, pos=%d, id=%d", position, id));
        setProcessed("HaruListActivity#onItemLongClick");
        return false;
    }
    
    @Event(clazz=AdapterView.OnItemSelectedListener.class, id=android.R.id.list)
    void onItemSelected(AdapterView<?> parent, View view, int position, long id){
        Log.d(getClass().getSimpleName(), String.format("onItemSelected, pos=%d, id=%d", position, id));
        setProcessed("HaruListActivity#onItemSelected");
    }
    
    @Event(clazz=AdapterView.OnItemSelectedListener.class, id=android.R.id.list)
    void onNothingSelected(AdapterView<?> parent){
        Log.d(getClass().getSimpleName(), "onNothingSelected");
        setProcessed("HaruListActivity#onNothingSelected");
    }
    
    void setProcessed(final String name){
        Editor editor = mPref.edit();
        editor.putBoolean(name, true);
        editor.commit();
    }
}
